package com.seeTools.boot.pc.controller.sys;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seeTools.boot.api.entity.sys.SysDictType;
import com.seeTools.boot.api.excel.utils.ExcelUtil;
import com.seeTools.boot.common.annotation.Log;
import com.seeTools.boot.common.core.PageQuery;
import com.seeTools.boot.common.core.Result;
import com.seeTools.boot.common.enums.BusinessType;
import com.seeTools.boot.pc.service.sys.SysDictTypeService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author: seeTools
 * @Date: 2023/11/1  17:04
 * @Version 1.0
 */
@Validated
@RestController
@RequestMapping("/dict/type")
public class SysDictTypeController {

    @Resource
    private SysDictTypeService sysDictTypeService;


    /**
     * 查询字典类型列表
     */
    @SaCheckPermission("dict.list")
    @PostMapping("/list")
    public Result<Page<SysDictType>> list(@RequestBody PageQuery<SysDictType> pageQuery) {
        Page<SysDictType> pageInfo = sysDictTypeService.findList(pageQuery);
        return Result.success(pageInfo);
    }

    /**
     * 查询字典类型详细
     *
     * @param dictId 字典ID
     */
    @GetMapping(value = "/{dictId}")
    public Result<SysDictType> getInfo(@PathVariable Long dictId) {
        return Result.success(sysDictTypeService.getById(dictId));
    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/optionSelect")
    public Result<List<SysDictType>> optionSelect() {
        return Result.state(sysDictTypeService.list());
    }

    /**
     * 导出字典类型列表
     */
    @Log(title = "字典类型", businessType = BusinessType.EXPORT)
    @SaCheckPermission("dict.export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDictType dictType) {
        List<SysDictType> list = sysDictTypeService.selectDictTypeList(dictType);
        ExcelUtil.exportExcel(list, "字典类型", SysDictType.class, response);
    }

    /**
     * 删除字典类型
     *
     * @param dictIds 字典ID串
     */
    @SaCheckPermission("dict.del")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/")
    public Result<Void> remove(@RequestBody SysDictType sysDictType) {
        return Result.state(sysDictTypeService.removeById(sysDictType));
    }

    /**
     * 新增字典类型
     *
     * @param sysDictType
     * @return
     */
    @SaCheckPermission("dict.add")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @PostMapping
    public Result<Void> add(@Validated @RequestBody SysDictType sysDictType) {
        if (!sysDictTypeService.checkDictTypeUnique(sysDictType)) {
            return Result.error("新增字典'" + sysDictType.getDictName() + "'失败，字典类型已存在");
        }
        sysDictTypeService.save(sysDictType);
        return Result.success();
    }

    /**
     * 修改字典类型
     *
     * @param sysDictType
     */
    @SaCheckPermission("dict.edit")
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @PutMapping()
    public Result<Void> update(@RequestBody SysDictType sysDictType) {
        return Result.state(sysDictTypeService.updateOnLock(sysDictType));
    }


    /**
     * 刷新字典缓存
     */
    @SaCheckPermission("dict.remove")
    @Log(title = "字典类型", businessType = BusinessType.CLEAN)
    @DeleteMapping("/refreshCache")
    public Result<Void> refreshCache() {
        sysDictTypeService.resetDictCache();
        return Result.success();
    }
}
